package org.dk.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import org.dk.domain.DataResult;
import org.dk.em.DataResultEnum;
import org.dk.pageInfo.PageInfo2;
import org.dk.pojo.TbContent;
import org.dk.pojo.TbContentCategory;
import org.dk.service.ConCateService;
import org.dk.service.TbContentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("contentController")
public class ContentController {

    @Reference
    private TbContentService tbContentService;
    @Reference
    private ConCateService conCateService;


    @RequestMapping("toContent")
    public String toContent(){
           return "admin/content";
       }

    @RequestMapping("selectContent")
    @ResponseBody
    public DataResult selectContent(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize){
        try {
            PageInfo2 page  = tbContentService.selectContent(pageNum,pageSize);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(page);
        }catch (Exception e){
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }


    @RequestMapping("addContent")
    @ResponseBody
    public DataResult addContent(TbContent content){
       /* String imgRedis = ConstantUtils.IMG_REDIS;
        redisTemplate.delete(imgRedis);*/
        try {
            tbContentService.addContent(content);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS);
        }catch (Exception e){
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }

    @RequestMapping("deleteContent")
    @ResponseBody
    public DataResult deleteContent(@RequestParam("ids[]") Long[] ids){
       /* String imgRedis = ConstantUtils.IMG_REDIS;
        redisTemplate.delete(imgRedis);*/
        try {
            tbContentService.deleteContent(ids);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS);
        }catch (Exception e){
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }

    @RequestMapping("updateContentReturn")
    @ResponseBody
    public DataResult updateContentReturn(Long id){
        /*String imgRedis = ConstantUtils.IMG_REDIS;
        redisTemplate.delete(imgRedis);*/
        try {
            TbContent content = tbContentService.selectById(id);
            List<TbContentCategory> list = conCateService.selectConCate(null);
            Map<String,Object> map = new HashMap<>();
            map.put("content",content);
            map.put("list",list);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(map);
        }catch (Exception e){
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }

    @RequestMapping("updateContent")
    @ResponseBody
    public DataResult updateContent(TbContent content){
        try {
            tbContentService.updateContent(content);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS);
        }catch (Exception e){
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }
}
